package com.ecnu.jiagou.damai.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ecnu.jiagou.damai.entity.City;
import com.ecnu.jiagou.damai.entity.Performance;
import com.ecnu.jiagou.damai.entity.Session;
import com.ecnu.jiagou.damai.entity.Show;
import com.ecnu.jiagou.damai.entity.Ticket;
import com.ecnu.jiagou.damai.mapper.ShowMapper;
import com.ecnu.jiagou.damai.result.Result;

import com.ecnu.jiagou.damai.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ShowService {
    
    @Autowired
    private ShowMapper showMapper;

    @Autowired
    private RedisUtil redisUtil;

    public Result<List<Show>> getShowByType(int type, String city){
        Map<String, Object> map = new HashMap<>();
        map.put("city", city);
        map.put("type", type);
        List<Show> list = showMapper.getShowByType(map);
        return Result.success(list);
    }

    public Result<List<Show>> getShowByTop(String city){
        List<Show> list = showMapper.getShowByTop(city);
        return Result.success(list);
    }

    public Show getShow(Long id){
        Show show = showMapper.getShowById(id);
        List<Session> sessions = showMapper.getSessions(id);
        show.setSessions(sessions);
        show.setDate(sessions.get(0).getDate().toString());
        Map<String,List<Ticket>> tickets = new HashMap<>();
        for(Session s : sessions){
            List<Ticket> t = showMapper.getTickets(s.getSessionid());
            tickets.put(s.getSessionid().toString(), t);
        }
        show.setTickets(tickets);
        //System.out.println(show.getTitle());
        //System.out.println(showMapper.getCitys(show.getTitle().toString()).size());
        List<City> citys = showMapper.getCitys(show.getTitle());
        show.setCitylist(citys);

        return show;
    }

    public List<Performance> getAllShows(){
        return showMapper.getShows();
    }

    public List<Performance> queryShows(String city, Integer type, String start, String end, String words){
        Map<String, Object> map = new HashMap<>();
        map.put("city", city);
        map.put("type", type);
        map.put("start", start);
        map.put("end", end);
        map.put("words", words);
        List<Performance> list = showMapper.queryShows(map);
        //redisUtil.set("Performance_total_Size",list.size());
        return list;
    }

    public Result<List<Show>> getShowByLike(int page){
        int total;
        if(redisUtil.get("Performance_total_Size") == null){
            total = showMapper.getCount();
            //System.out.println(total);
            redisUtil.set("Performance_total_Size",total);
        }else{
           total =  Integer.parseInt(redisUtil.get("Performance_total_Size").toString());
        }
        int offset = 0;
        int count = total - page * 10;
        if(count < 3){
            offset = (page - 1) * 10;
        }else {
            offset = page * 10;
        }
        List<Show> list = showMapper.getShowLike(offset);
        return Result.success(list);
    }

    public Result<Performance> getDetail(int ticketid){
        return Result.success(showMapper.getDetail(ticketid));
    }

}
